package ex2.employee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CEmployeeInputReader {
	
	private CEmployeeHandler _employeeHandler;
	private IEquipmentHandler _equipmentHandler;
	
	public CEmployeeInputReader(CEmployeeHandler employeeHandler,IEquipmentHandler equipmentHandler ){
		this._employeeHandler = employeeHandler;
		this._equipmentHandler = equipmentHandler;
	}
	
	public void readInformationFromCommandLine(){
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String tEmployeeRecord = null;
		
		
		System.out.println("Enter employees information");
		
		try
		 {
			tEmployeeRecord= br.readLine();
		 }
		catch(IOException e){
			e.printStackTrace();
		}
		
		
		while(!tEmployeeRecord.equals("STOP")){
			
			IEmployee tEmployee = null;
			
			switch (recordType(tEmployeeRecord)){
				case 0:
					
					try{
						tEmployee = new CEmployee(tEmployeeRecord, this._employeeHandler );
					}
					catch (IllegalArgumentException e){
						malformedRecordInput();
						break;
					}
					
					this._employeeHandler.addEmployee(tEmployee);
					
					break;
				case 1:
					
					try{
						tEmployee = new CManager(tEmployeeRecord, this._employeeHandler);
					}
					catch (IllegalArgumentException e){
						malformedRecordInput();
						break;
					}
					
					this._employeeHandler.addEmployee(tEmployee);
					break;
					
				case 2:
						
					try{
						tEmployee = new CSpecialist(tEmployeeRecord, this._employeeHandler, this._equipmentHandler);
					}
					catch (IllegalArgumentException e){
						malformedRecordInput();
						break;
					}
					
					this._employeeHandler.addEmployee(tEmployee);
					break;
					
				default:
					malformedRecordInput();
			}
			
			try
			 {
				tEmployeeRecord= br.readLine();
			 }
			catch(IOException e){
				e.printStackTrace();
			}
				
		}

	}
	
	
	private int recordType(String record){
		int tAnswer = -1;
		String tTrimmedRecord = record.trim();
		
		if (tTrimmedRecord.startsWith("0")) return 0;
		if (tTrimmedRecord.startsWith("1")) return 1;
		if (tTrimmedRecord.startsWith("2")) return 2;
		
		return tAnswer;
	}
	
	private void malformedRecordInput(){
		System.out.println("Malformed record inserted");
	}

}
